#pragma once

#pragma warning(disable : 4996)

#include <atlstr.h>
#include <string>
using namespace std;

namespace PortUtility
{
	typedef enum _PORT_TYPE
	{
		ESerial,
		EParaellel,
		EUSB,
		EPORT_TYPE_COUNT
	}EPORT_TYPE;

	typedef struct _Serial_Port_Info
	{
		CString strCom;
		int nPortIndex;
		int nParity;	// 0: None, 1:EVEN, 2: ODD, 
		int nBoudRate; //1:4800, 2:9600,...6:115200
		int nStopBit;
		int nDataBit;
		int nFlowControl;
		HANDLE hCom;
		OVERLAPPED oWrite;
		OVERLAPPED oRead;
	}Serial_Port_Info;
	

	typedef struct _DeviceDefinition
	{
		CString DeviceName;
		CString DeviceLDN;
		CString DeviceClass;
		EPORT_TYPE ePortType;
		Serial_Port_Info PortSetting;

	}DeviceDefinition;

	//////////////////////////////////////////////////////////////////////////
	// Error code & description
	const static int ErrorAccount = 5;
	const static CString ErrorDes[ErrorAccount] = {
		_T("Normal"),
		_T("Fail to open port"),
		_T("Unknown"),
		_T("Unknown"),
		_T("Unknown"),
	};

	// COM R/W
	HANDLE OpenCom(int nCom, OVERLAPPED &oWrite, OVERLAPPED &oRead, int nParity, int &nError);
	HANDLE OpenCom(const CString& strCom, OVERLAPPED &oWrite, OVERLAPPED &oRead, int nParity, int &nError);

	BOOL PR_SendStr2Com(HANDLE hCom, LPCTSTR lpStr, int nLength, OVERLAPPED &oWrite);
	BOOL PR_SendStr2Com(HANDLE hCom, char *pStr, int nLength, OVERLAPPED &oWrite);

	BOOL PR_ReadStrFromCom(HANDLE hCom, LPTSTR pStr, int nLen, OVERLAPPED &oRead);
	BOOL PR_ReadStrFromCom(HANDLE hCom, char *pStr, int nLen, OVERLAPPED &oRead);

	BOOL PR_CloseCom(HANDLE hCom, OVERLAPPED &oWrite, OVERLAPPED &oRead);

	// LPT R/W
	HANDLE PR_OpenLPT(int nLPT, OVERLAPPED &oWrite, OVERLAPPED &oRead, int &nError);

	BOOL PR_CloseLPT(HANDLE hLPT, OVERLAPPED &oWrite, OVERLAPPED &oRead);

	BOOL PR_SendStr2LPT(HANDLE hCom, char* lpStr, int nLength, OVERLAPPED &oWrite);

	BOOL PR_ReadStrFromLPT(HANDLE hCom, char* szBuf, int nLen, OVERLAPPED &oRead);


	string wstring2string(wstring wstr);
}
